home *** CD-ROM | disk | FTP | other *** search
- /*
- * Stream filter to decode printable ASCII back into 8 bit bytes from
- * survive most networks -- especially the RSCS community which seems
- * to gobble up all sorts of the "ordinary" characters.
- * Encoding is into a 64 character set "[A-Z][a-z][0-9]+-". This means
- * that using 4 characters we can safely represent 3 bytes. Some overhead
- * but I can take it.
- */
-
- #include <stdio.h>
- #include "coder.h"
- char *myname;
-
- main(argc, argv)
- char **argv;
- {
- register int c, word, bcount;
- register FILE *fin = stdin, *fout = stdout; /* in regs for speed */
- register char *map, *p;
- int w;
- char buf[512];
- extern char *index();
-
- myname = argv[0];
- if (argc > 2)
- fprintf(stderr, "Usage: %s [file]\n", myname), exit(1);
- if (argc == 2 && (fin = fopen(argv[1], "r")) == NULL) {
- fprintf(stderr, "%s: ", myname);
- perror(argv[1]);
- exit(1);
- }
- /* skip to beginning of encoded data */
- do {
- if (fgets(buf, sizeof buf, fin) == NULL)
- fatal("Missing header");
- /* trim trailing blanks (sigh) */
- p = index(buf, '\n');
- if (p == 0)
- continue;
- while (*--p == ' ')
- ;
- p[1] = '\n';
- p[2] = '\0';
- } while (strcmp(buf, header) != 0);
-
- /* define input mapping table */
- map = buf+1;
- for (c = 0; c < 256; c++)
- map[c] = 64; /* illegal */
- for (c = 0; c < 64; c++)
- map[ENCODE(c)] = c;
- map[EOF] = 65; /* special cases */
- map['/'] = 66;
-
- word = 0;
- bcount = 4;
- for (;;) {
- c = map[getc(fin)];
- if ((unsigned)c < 64) {
- word <<= 6;
- word |= c;
- if (--bcount == 0) {
- putc((word >> 16) & 0xFF, fout);
- putc((word >> 8) & 0xFF, fout);
- putc((word) & 0xFF, fout);
- word = 0;
- bcount = 4;
- }
- continue;
- }
- switch (c) {
- default:
- /*
- * Ignore stuff not in the code set.
- * I don't like this.
- */
- continue;
- case 65: /* EOF */
- fatal("Unexpected EOF");
- case 66: /* '/' */
- /* trailer follows: %d%x */
- c = getc(fin) - '0';
- if ((unsigned)c >= 3) /* must be 0,1,2 */
- fatal("Corrupted input (trailer)");
- if (fscanf(fin, "%x", &w) != 1)
- fatal("Corrupted input (trailer)");
- switch (c) {
- case 2: putc((w >> 8) & 0xFF, fout);
- case 1: putc((w ) & 0xFF, fout);
- }
- exit(0);
- }
- }
- }
-
- fatal(s)
- char *s;
- {
- fprintf(stderr, "%s: %s\n", myname, s);
- exit(1);
- }
-